<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/26
 * Time: 18:29
 */
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller{
    public function index(){
        if(!IS_POST){
            $this->display();
        }else{

            $post = I("post.");
            $bol = M("users")->where("account='{$post['username']}' and password=sha1('{$post['password']}')")->select();
            if ($bol) {

                    $ip = $_SERVER['REMOTE_ADDR'];
                    $data = ["status"=>1,"last_session_id"=>session_id(),"log_time"=>time(),"last_login_ip"=>$ip];
                    M("users")->where("id={$bol[0]['id']}")->save($data);
                    $bol=M("users")->where("id={$bol[0]['id']}")->select();
                    $_SESSION["users"] = $bol[0];
                    $this->success( "登录成功",U("Personage/index/"),0);

            } else {
                $this->error("账号或密码不对", U("login/index"), 3);
            }

        }
    }
//生成验证码
    public function yzm(){
        $str ='0123456789';
        $verify = new \Think\Verify(['codeSet'=>$str, 'imageH' =>40,'imageW'=>110, 'fontSize'=>15,'length'=>4,'useNoise'  => false,'reset' => false, 'expire'=> 1800]);
        $verify->entry($_GET['id']);
    }
    //验证码ajax检测
    public function check(){
        $verify = new \Think\Verify();
        $bool = $verify->check($_GET['yzm'],$_GET['id']);
        if($bool){
            echo 1;
        }else{
            echo 0;
        }
    }
    //退出登录
    public function quit(){
        $_SESSION['users']="";
        M("users")->where("id={$_GET['id']}")->save(["status"=>0]);
        $this->redirect("Index/index");
    }
    //检测注册账号
    public function accunt(){
       $acc = str_replace(" ","",I("get.account"));
        $arr = M("users")->where("account='{$acc}'")->select();
        if($arr) {
            echo 0;
        }else{
            echo 1;
        }

    }
    public function _empty(){
        $this->display("Public:404");
    }
}